// pages/user/user.js
import {
    Address
} from './addAddress-model.js';

const app = getApp()
const $address = new Address()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        dialogShow: false,
        threeArea: {
            provice: null,
            city: null,
            area: null
        },
        default: true,
        hasCity: true,
        hasArea: true,
        editAddress: null,
        editRegion: [],
        addressList: null,
        isShowPopup: false,
        tips: '',
        toggleWord: '新增',
        editId: '',
        loadDone: false,
        editStatusReset: false,
        type: '',
        orderId: '',
        addressId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        let that = this
        wx.showLoading({
            title: '加载中',
        })
        // 判断是否是从选择地址过来的
        if (options.type) {
            this.setData({
                type: options.type,
                orderId: options.orderId,
                addressId: options.addressId
            })
        }
        let provice = {
            "data": {
                "pid": "0"
            }
        }

        // 获取省一级 pid 0
        $address.getThree(provice, res => {
            if (res.status) {
                that.setData({
                    'threeArea.provice': res.data
                })
                wx.hideLoading()

            } else {
                wx.hideLoading()
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
            }
        })

    },
    //   是否默认地址按钮
    switchChange(e) {
        console.log(e.detail.value)
        this.setData({
            default: e.detail.value
        })
    },
    preventD(e) {
        return;
    },
    // 省选择
    proviceChange(e) {
        console.log(e.detail.value)
        let val = e.detail.value
        let provice = this.data.threeArea.provice[val]
        let that = this
        let params = {
            "data": {
                "pid": provice.id
            }
        }
        $address.getThree(params, res => {
            if (res.status) {
                that.setData({
                    'threeArea.city': res.data,
                    'editRegion[1]': '',
                    'editRegion[2]': '',
                })
            }
        })
        this.setData({
            'editRegion[0]': {
                id: provice.pid,
                name: provice.name,
                index: val
            },
            hasCity: false,
            hasArea: true

        })
        if (this.data.toggleWord == '编辑') {
            this.setData({
                'editAddress.detailedAddress': '',
                editStatusReset: true
            })
        }
    },
    // 市选择
    cityChange(e) {
        console.log(e.detail.value)
        let that = this
        let val = e.detail.value
        let city = this.data.threeArea.city[val]
        let params = {
            "data": {
                "pid": city.id
            }
        }
        $address.getThree(params, res => {
            if (res.status) {
                that.setData({
                    'threeArea.area': res.data,
                    'editRegion[2]': '',

                })
            }
        })
        this.setData({
            'editRegion[1]': {
                id: city.pid,
                name: city.name,
                index: val
            },
            hasArea: false,
        })
        if (this.data.toggleWord == '编辑') {
            this.setData({
                'editAddress.detailedAddress': '',
                editStatusReset: true
            })
        }
    },
    // 区域选择
    areaChange(e) {
        console.log(e.detail.value)
        let val = e.detail.value
        let area = this.data.threeArea.area[val]

        this.setData({
            'editRegion[2]': {
                id: area.pid,
                name: area.name,
                index: val
            },
        })
        if (this.data.toggleWord == '编辑') {
            this.setData({
                'editAddress.detailedAddress': '',
                editStatusReset: true
            })
        }
    },
    // 新增编辑地址
    formSubmit(e) {
        console.log(e)
        let formdata = e.detail.value
        let that = this
        console.log(formdata)
        if (formdata.acceptName.trim() == '') {
            wx.showToast({
                title: '请输入收货人',
                icon: 'none'
            })
        } else if (formdata.acceptName.length > 50) {
            wx.showToast({
                title: '收货人不超过50个字',
                icon: 'none'
            })
        } else if (formdata.phone.trim() == '' || !/^((0\d{2,3}-\d{7,8})|(1[345678]\d{9}))$/.test(formdata.phone)) {
            wx.showToast({
                title: '请输入正确的联系电话',
                icon: 'none'
            })
        } else if (formdata.city == null || formdata.provice == null || formdata.provice === '' || formdata.city === '') {
            // else if (formdata.area == null || formdata.city == null || formdata.provice == null || formdata.area === '' || formdata.city === '' || formdata.provice === '') {
            wx.showToast({
                title: '请选择省市',
                icon: 'none'
            })
        } else if (formdata.detail.trim() == '') {
            wx.showToast({
                title: '请输入详细地址',
                icon: 'none'
            })
        } else if (formdata.detail.length > 100) {
            wx.showToast({
                title: '收货地址不超过100个字',
                icon: 'none'
            })
        } else {
            let data = this.data
            let threeArea = data.threeArea
            console.log(formdata.city, formdata.area);

            let area = formdata.area != null ? threeArea.area[formdata.area].name : '' + ' ';

            let PCA = threeArea.provice[formdata.provice].name + ' ' + threeArea.city[formdata.city].name + ' ' + area + ' ';
            console.log(PCA);
            let areaId = formdata.area != null ? data.threeArea.area[formdata.area].id : '';

            let params = {
                data: {
                    "province": "" + data.threeArea.provice[formdata.provice].id,
                    "city": "" + data.threeArea.city[formdata.city].id,
                    "area": "" + areaId,
                    "detailedAddress": PCA + formdata.detail,
                    "contacts": formdata.acceptName,
                    "mobilePhone": formdata.phone,
                    "defaultOptions": data.default ? 1 : 0
                }
            }

            console.log('params', params)
            wx.showModal({
                title: '提示',
                content: '确认保存吗？',
                success: function (res) {
                    if (res.confirm) {
                        $address.addUserAddress(params, res3 => {
                            console.log('新增', res3)
                            let status = res3.status
                            if (status) {
                                wx.showToast({
                                    title: '新增成功',
                                    icon: 'success',
                                    duration: 1500,
                                    success() {
                                        var pagelist = getCurrentPages();
                                        if (pagelist.length > 1) {
                                            //获取上一个页面实例对象
                                            var prePage = pagelist[pagelist.length - 2];
                                            //如果是订单详情跳转过来的
                                            if (data.type == 'sel') {
                                                prePage.onLoad({
                                                    orderId: data.orderId,
                                                    addressId: data.addressId
                                                });
                                            } else {
                                                prePage.onLoad();
                                            }
                                            wx.navigateBack({
                                                delta: 1
                                            })

                                        }
                                    }
                                })
                            } else {
                                wx.hideLoading()
                                wx.showToast({
                                    title: '服务器错误',
                                    icon: 'none'
                                })
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    }
})